﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLMemberPointMS.Common;
using DLMemberPointMS.Entity;
using DLMemberPointMS.Entity.POCOModel;
using DLMemberPointMS.Entity.ViewModel;
using static DLMemberPointMS.Common.EnumType;

namespace DLMemberPointMS.Business
{
   public class UserService:BaseService<Users>
    {
        //李晓婉写的用户管理
        #region 用户表分页多条件查询
        public OperateResult GetUserPage(UserPageSelect view)
        {
            var query = PredicateExtensions.True<Users>();
            if (!string.IsNullOrWhiteSpace(view.U_LoginName))
            {
                query = query.And(e => e.U_LoginName.Contains(view.U_LoginName));
            }
            if (!string.IsNullOrWhiteSpace(view.U_RealName))
            {
                query = query.And(e => e.U_RealName.Contains(view.U_RealName));
            }
             if (!string.IsNullOrWhiteSpace(view.U_Telephone))
            {
                query = query.And(e => e.U_Telephone.Contains(view.U_Telephone));
            }
            int rowCount = 0;
            var pageData = GetList(view.page, view.rows, ref rowCount, query, e => e.U_ID, true)
                .Select(e => new
                {
                    e.U_ID,
                    e.U_LoginName,
                    e.U_RealName,
                      e.U_Sex,
                    e.U_Telephone,
                    U_Role= ((URoleenum)e.U_Role).ToString(),
                    e.U_CanDelete
                }).ToList();
            if (pageData == null)
            {
                return new OperateResult(code.Error, "没有数据哟！");
            }
            else
            {
                return new OperateResult(code.Success,"查询成功！", pageData,rowCount);
            }
        }
        #endregion
        #region 插入
        public OperateResult InsertUser(UserInsertView view)
        {

            if (string.IsNullOrEmpty(view.U_LoginName))
            {
               
                return new OperateResult(code.Error, "会员名不能为空");
            }
            if (Find(e => e.U_LoginName == view.U_LoginName) != null)
            {
                return new OperateResult(code.Error, "该会员已存在");
            }
            if (string.IsNullOrEmpty(view.U_RealName))
            {
                return new OperateResult(code.Error, "真实姓名不能为空");
            }
            else
            {
                Users model = new Users();
                model.U_LoginName = view.U_LoginName;
                model.U_CanDelete = view.U_CanDelete;
                model.U_RealName = view.U_RealName;
                model.U_Role = view.U_Role;
                model.U_Sex = view.U_Sex;
                model.U_Telephone = view.U_Telephone;
                model.U_Password = "1";
                if (Add(model))
                {
                    return new OperateResult(code.Success, "会员添加成功");
                }
                else
                {
                    return new OperateResult(code.Error, "会员添加失败");
                }
            }
                  
              

        }
        #endregion
        #region 修改
        public OperateResult UserUpdateSelect(int id)
        {
            var model = Find(e => e.U_ID == id);
            if (model != null)
            {
                var mo = new UserInsertView();
                mo.U_ID = model.U_ID;
                mo.U_LoginName = model.U_LoginName;
                mo.U_Password = model.U_Password;
                mo.U_RealName = model.U_RealName;
                mo.U_Role = (int)model.U_Role;
                mo.U_Sex = model.U_Sex;
                mo.U_Telephone = model.U_Telephone;
                mo.U_CanDelete =Convert.ToBoolean(model.U_CanDelete);
                return new OperateResult(code.Success, "修改成功", mo);
            }
            else
            {
                return new OperateResult(code.Error, "修改失败");
            }
        }
        public OperateResult UserUpdate(UserInsertView view)
        {
            var model = Find(e => e.U_ID == view.U_ID);
            if (model == null)
            {
                return new OperateResult(code.Error, "数据不存在");
            }
            if (view.U_LoginName == null)
            {
                return new OperateResult(code.Error, "登录名不能为空");
            }
            if (view.U_RealName == null)
            {
                return new OperateResult(code.Error, "真实姓名不能为空");
            }
            else
            {
                model.U_CanDelete = view.U_CanDelete;
                model.U_ID = view.U_ID;
                model.U_LoginName = view.U_LoginName;
                model.U_Password = view.U_Password;
                model.U_RealName = view.U_RealName;
                model.U_Role = (int)view.U_Role;
                model.U_Sex = view.U_Sex;
                model.U_Telephone = view.U_Telephone;
                if (Update(model))
                {
                    return new OperateResult(code.Success, "修改成功", model);
                }
                else
                {
                    return new OperateResult(code.Error, "修改失败");
                }
            }
           }
        #endregion
        #region 删除
        public OperateResult UserDelete(int U_ID)
        {
            var model = Find(e => e.U_ID == U_ID);
            if (model != null)
            {

                if (Delete(model))
                {
                    return new OperateResult(code.Success, "删除成功");
                }
                else
                {
                    return new OperateResult(code.Error, "删除失败");
                }
            }
            else
            {
                return new OperateResult(code.Error, "数据不存在");
            }
         
        }
        #endregion

    }
}
